﻿// ## 2023/05/03 # 江东新风 # 函数主体自305迁回 ##
// ## 2021/10/10 # 江东新风 # 将函数内容全搬运到305，以便其他cpp调用 ##
// ## 2021/10/01 # 江东新风 # namespace的韩文改成英文 ##
// ## 2021/09/15 # 江东新风 # 更改pk::core[]函数为英文##
// ## 2021/03/05 # 白马叔叔 # 修正巡查特技倍率 ##
// ## 2020/10/27 # 江东新风 # 新te ##
// ## 2020/07/26 ##
namespace INSPECTIONS
{
	class Main
	{
		Main()
		{
			pk::set_func(104, pk::func104_t(callback));
		}

		int callback(pk::building @building, const pk::detail::arrayptr<pk::person @> &in actors)
		{
			// pk::trace("actors size:" + pk::Sizeof(actors));
			if (actors[0] !is null) // 这里判断的话，其他地方调用内政函数还得再内政函数里再判断一次
			{
				pk::list<pk::person @> actor_list;
				for (int i = 0; i < actors.length; i++)
				{
					actor_list.add(actors[i]);
				}

				return get_inspections_order_inc(building, actor_list);
			}
			return 0;
		}

	}

	Main main;

	int get_inspections_order_inc(pk::building @building, pk::list<pk::person @> actors)
	{
		if (pk::is_alive(building) and actors[0] !is null)
		{
			bool flag = false;
			if (building.get_id() < 据点_末)
			{
				int n = 0;
				int sum = 0;
				int max = 0;
				for (int i = 0; i < actors.count; i++)
				{
					pk::person @actor = actors[i];
					if (pk::is_alive(actor))
					{
#if param_set
						int s = actor.stat[pk::get_patrol_param().stat];
#endif
#if not_param_set
						int s = actor.stat[int(pk::core["inspection.stat"])];
#endif					
						sum += s;
						max = pk::max(max, s);
						if (ch::has_skill(actor, 特技_巡查))
							flag = true;
					}
				}

				float exp_eff = 1.0f;
				float absent_eff = 1.0f;
				float expend_eff = 1.0f;
				if (auto_inner::is_auto_affairs(building, actors)) // 只有在人数为1，开启自动内政，且有执政官经验时享受加成
				{
					// exp_eff = main.get_exp_eff(actors[0].get_id());
					exp_eff = auto_inner::get_exp_eff(actors[0].get_id(), 内政自动化_巡查);
					absent_eff = auto_inner::get_absent_eff(actors[0], 内政自动化_巡查);

					int service_id = actors[0].service;
					if (pk::is_valid_base_id(service_id))
					{
						auto building_p = ch::get_baseIA(service_id);
						// 由于一个人巡查的效果明显差太多,会导致玩家的治安补不上来,因此给予一定的补正
						float rate = float(building_p.porder_effic) / 100 * 2 * 1.2; // 执政官经验倍率由于无法互动而降低了,在这里暂时补上这部分损失的值
						expend_eff = pk::min(5.0f, rate);
					}
					else
						expend_eff = 0.0;
				}

				// pk::info(pk::format("exp_eff: {}, absent_eff: {}, expend_eff: {}", exp_eff, absent_eff, expend_eff));
				//  三个能力100,10+2，三个120 12+2，三个70,7+2,1个120 4+2，1个70 2+2
				//  改后，3个100,400/37 10+1, 120*2/37+1
				n = int((sum + max) * exp_eff * absent_eff * expend_eff / 37 + 1); // 原来是 sum/28+2

				int service_id = actors[0].service;
				if (ch::get_auto_affairs_status(building) and actors.count == 1 and pk::is_valid_base_id(service_id))
				{

					if (ch::get_baseIA(service_id).porder_effic == 0)
						n = 0;
				}

				// 兵临城下训练效果仅有一半
				if (pk::enemies_around(building))
				{
					n = n * 兵临城下时巡查倍率 / 100;
				}
				// if (!building.is_player())
				// 	n *= 2; // ai优待

				if (flag)
					n = n * pk::core::skill_constant_value(特技_巡查) / 100;
				if (ch::get_auto_affairs_status(building) and actors.count == 1)
					return int(n);
				int max_order = 100;
				int order = 0;
				if (building.get_id() < 城市_末)
					order = pk::building_to_city(building).public_order;
				else
					order = ch::get_base_p(building.get_id()).public_order;
				if (order + n > max_order)
					n = max_order - order;
				return int(n);
			}
		}

		return 0;
	}
}